// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UcDbDiffPropertyList.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The uc db diff property list.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.WinClient.Forms.DbItems
{
    using System;
    using System.Data;
    using System.Windows.Forms;
    using Common.Helper;

    /// <summary>
    /// The uc db diff property list.
    /// </summary>
    public class UcDbDiffPropertyList : UcDbListBase
    {
        /// <summary>
        /// The column header property name.
        /// </summary>
        private ColumnHeader columnHeaderPropertyName;

        /// <summary>
        /// The column header value.
        /// </summary>
        private ColumnHeader columnHeaderValue;

        /// <summary>
        /// The lv properties.
        /// </summary>
        private ListView lvProperties;

        /// <summary>
        /// The panel title.
        /// </summary>
        private Panel panelTitle;

        /// <summary>
        /// Initializes a new instance of the <see cref="UcDbDiffPropertyList"/> class.
        /// </summary>
        public UcDbDiffPropertyList()
        {
            // This call is required by the Windows Form Designer.
            this.InitializeComponent();
            this.lvProperties.BorderStyle = BorderStyle.None;
        }

        #region Designer generated code

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
            }

            base.Dispose(disposing);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        ///   the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lvProperties = new ListView();
            this.columnHeaderPropertyName = new ColumnHeader();
            this.columnHeaderValue = new ColumnHeader();
            this.panelTitle = new Panel();
            this.panelTitle.SuspendLayout();
            this.SuspendLayout();
            this.panelTitle.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        #endregion

        #region SetData

        /// <summary>
        /// The set data.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="itemTypeInfo">
        /// The item type info.
        /// </param>
        /// <exception cref="ApplicationException">
        /// </exception>
        public override void SetData(DataTable data, DatabaseObjectTypes itemTypeInfo)
        {
            this._itemTypeInfo = itemTypeInfo;
            if (data.Rows.Count != 1)
                throw new ApplicationException("ERROR: Invalid data!");

            this.PopulatePropertyList(data.DefaultView);
        }

        /// <summary>
        /// The set data.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="itemTypeInfo">
        /// The item type info.
        /// </param>
        /// <exception cref="ApplicationException">
        /// </exception>
        public override void SetData(DataView data, DatabaseObjectTypes itemTypeInfo)
        {
            this._itemTypeInfo = itemTypeInfo;
            if (data.Count != 1)
                throw new ApplicationException("ERROR: Invalid data!");

            this.PopulatePropertyList(data);
        }

        /// <summary>
        /// The set data.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="filterExpression">
        /// The filter expression.
        /// </param>
        /// <param name="itemTypeInfo">
        /// The item type info.
        /// </param>
        /// <exception cref="ApplicationException">
        /// </exception>
        public override void SetData(DataTable data, string filterExpression, DatabaseObjectTypes itemTypeInfo)
        {
            this._itemTypeInfo = itemTypeInfo;
            if (data.Rows.Count != 1)
                throw new ApplicationException("ERROR: Invalid data!");

            this.PopulatePropertyList(data.DefaultView);
        }

        #endregion SetData

        /// <summary>
        /// The populate property list.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        private void PopulatePropertyList(DataView data)
        {
            ListViewItem lvi;
            this.lvProperties.Items.Clear();
            foreach (DataColumn column in data.Table.Columns)
            {
                lvi = new ListViewItem(new[]
                                           {
                                               column.ColumnName, 
                                               data[0][column.ColumnName].ToString()
                                           });
                this.lvProperties.Items.Add(lvi);
            }
        }

        /// <summary>
        /// The lv properties_ resize.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args
        /// </param>
        private void lvProperties_Resize(object sender, EventArgs e)
        {
            this.lvProperties.Columns[0].Width = 200;
            this.lvProperties.Columns[1].Width = this.lvProperties.Width - this.lvProperties.Columns[0].Width - 14;
        }
    }
}